import axios from 'axios';
import _ from 'lodash';

import { mergeConfig } from '@/mergeConfig.js';

let serverConfig = null;
if (_.has(window, 'server_config')) {
  serverConfig = window.server_config.getServerConfig();
}

const CONFIG = mergeConfig(serverConfig);

let axiosMain = axios.create({
  timeout: 1000 * 10,
  baseURL: `${CONFIG.host}`,
});

axiosMain.defaults.headers.post['Content-Type'] =
  'Content-Type:application/x-www-form-urlencoded; charset=UTF-8'; //预防跨域

// 添加请求拦截器
axiosMain.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axiosMain.interceptors.response.use(
  function(response) {
    // 如果使用全局loading，可以在这里close，不过……
    // 对响应数据做点什么
    if (response.status === 200) {
      return response.data; //对明确的200做脱壳处理
    }
    // http://www.axios-js.com/zh-cn/docs/#%E5%93%8D%E5%BA%94%E7%BB%93%E6%9E%84 官网的响应结构
    return response;
  },
  function(error) {
    console.error(error);
    // 对响应错误做点什么
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = '请求错误(400)';
          break;
        case 401:
          this.$router.push({
            path: '/login',
            query: { ...this.$route.query },
          });
          break;
        case 403:
          error.message = '拒绝访问(403)';
          break;
        case 404:
          error.message = '请求出错(404)';
          break;
        case 408:
          error.message = '请求超时(408)';
          break;
        case 500:
          error.message = '服务器错误(500)';
          break;
        case 501:
          error.message = '服务未实现(501)';
          break;
        case 502:
          error.message = '网络错误(502)';
          break;
        case 503:
          error.message = '服务不可用(503)';
          break;
        case 504:
          error.message = '网络超时(504)';
          break;
        case 505:
          error.message = 'HTTP版本不受支持(505)';
          break;
        default:
          error.message = `连接出错(${error.response.status})!`;
      }
    } else {
      error.message = '连接服务器失败!';
    }
    return Promise.reject(error);
  }
);

let api = {
  用户身份验证并登陆: function(params, data) {
    // let url = '/api/WeChatRoom/ScrmZhiboUsersLogin';
    let url;
    let cdcid = _.get(params, 'companyDepartmentCompanyId', 0);
    if (cdcid == 0) {
      url = '/api/WeChatRoom/ScrmZhiboUsersLoginV2';
    } else {
      url = '/api/WeChatRoom/ScrmZhiboUsersLoginV3';
    }
    return axiosMain.post(url, data, { params: params });
  },
  进入房间: function(params, data) {
    let url = '/api/WeChatRoom/UserInRoom';
    return axiosMain.post(url, data, { params: params });
  },
  获取直播间具体内容: function(params) {
    let url = `/api/WeChatRoom/GetChatRoom`;
    return axiosMain.get(url, { params: params });
  }, // 获取指定直播间
  直播间发送消息: function(params, data) {
    let url = '/api/WeChatRoom/SendChatRoomMsg';
    return axiosMain.post(url, data, { params: params });
  },
  直播间点赞: function(params, data) {
    let url = '/api/WeChatRoom/SendChatRoomLike';
    return axiosMain.post(url, data, { params: params });
  },

  获取直播间list: function(params) {
    let url = '/api/WeChatRoom/GetScrmVideoRoomAPPR';
    return axiosMain.get(url, { params: params });
  }, // 获取直播间
};

export { axios, api };
